<?php
/*
 * Created on Dec 7, 2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

include_once("../configure/configure.php");		//--> global var
include_once("../lib/impl/KeywordImpl.php");
include_once("../admin/lib/utils/StringUtils.php");


set_time_limit(0);
header("Content-type:text/html; charset=utf-8");
   
createCouponIndex();






function createCouponIndex()
{
	$oKeywordImpl 	= new KeywordImpl(DB_TAG_COUPON);
	
	$maxIndexCouponId = $oKeywordImpl->getColumn(DB_TAG_COUPON, "SELECT max(couponId) FROM keywords_coupon_mapping");
	
	if(empty($maxIndexCouponId))
		$sql = "SELECT id, name, descript FROM coupon WHERE status = 1 ORDER BY id";
	else
		$sql = "SELECT id, name, descript FROM coupon WHERE status = 1 AND id > $maxIndexCouponId";
	
//	$sql = "SELECT id, name, descript FROM coupon WHERE status = 1 AND id NOT IN (SELECT DISTINCT couponId FROM keywords_coupon_mapping)";
	
	echo "sql = $sql \n";
	
	$allData = $oKeywordImpl->getAllData(DB_TAG_COUPON, $sql);
	
	echo 'waitting update calculate count = ' . count($allData) . '<br>';
	foreach($allData as $arrRow){
		$couponId 	= $arrRow['id'];
		$name		= strtolower($arrRow['name']);
		$descript	= strtolower($arrRow['descript']);
		
//		echo "***************************************************<br>";
		
//		echo "	original name = $name <br>";
		
		$name	= StringUtils::deleteExcessSpaces($name);
		$name	= trim($name);
		
		$descript	= StringUtils::deleteExcessSpaces($descript);
		$descript	= trim($descript);
		
		
		
		$arrData = array();
		
		if(!empty($name))
			$arrData[] = $name;
			
		if(!empty($descript))
			$arrData[] = $descript;
		
		foreach($arrData as $data){
//			echo "	prepare data ======================= = $data <br>";
			
			$arrWords = explode(' ', $data);
			
//			print_r($arrWords);
			
			$arrNewWords 	= array();
			
			// check is stop word
			foreach($arrWords as $word){
				$word = str_replace('\\', '', $word);
				$word = addslashes($word);
				
				$keywordId = $oKeywordImpl->getColumn(DB_TAG_KEYWORD, "SELECT id FROM keywords_stop WHERE keywords = '$word'");

				if(empty($keywordId)){
					$arrNewWords[] = $word;
				}
			}
			
//			print_r($arrNewWords);
			
			
			$len = count($arrNewWords);
			
			for($i = 0; $i < $len; $i++){
				$words = $arrNewWords[$i];
				$words = str_replace('\\', '', $words);
				$words = addslashes($words);
				
//				echo "	new words ======================= = $words <br>";
				$keywordId = $oKeywordImpl->getColumn(DB_TAG_COUPON, "SELECT id FROM keywords_coupon WHERE keywords = '$words'");
				
				if(empty($keywordId)){
					$words = str_replace('\\', '', $words);
					
					$sql_data_array = array(
						'keywords' 	=> $words
					);
					
					$keywordId = $oKeywordImpl->insertUpdate(DB_TAG_COUPON, 'keywords_coupon', $sql_data_array);
				}
				
//				echo "keywordId : $keywordId <br>";
				
				$sql_data_array = array(
					'couponId' 	=> $couponId,
					'keywordId' => $keywordId
				);
					
				$count += $oKeywordImpl->insertUpdate(DB_TAG_COUPON, 'keywords_coupon_mapping', $sql_data_array, 'insert', " ON DUPLICATE KEY UPDATE keywordId=Values(keywordId)");
			
//				echo "couponid : $couponId <br>";
				for($j = $i + 1; $j < $len; $j++){
					$words .= ' ';
					$words .= $arrNewWords[$j];
			
					$words = str_replace('\\', '', $words);
					$words = addslashes($words);
					
//					echo "	new words 2222 ======================= = $words <br>";
					
					if(strlen($words) > 32)
						$words = md5($words);
					
					
					$keywordId = $oKeywordImpl->getColumn(DB_TAG_COUPON, "SELECT id FROM keywords_coupon WHERE keywords = '$words'");
				
					if(empty($keywordId)){
						$words = str_replace('\\', '', $words);
						
						$sql_data_array = array(
							'keywords' 	=> $words
						);
						
						$keywordId = $oKeywordImpl->insertUpdate(DB_TAG_COUPON, 'keywords_coupon', $sql_data_array);
					}
					
//					echo "keywordId 222 : $keywordId <br>";
					
					$sql_data_array = array(
						'couponId' 	=> $couponId,
						'keywordId' => $keywordId
					);
						
					$count += $oKeywordImpl->insertUpdate(DB_TAG_COUPON, 'keywords_coupon_mapping', $sql_data_array, 'insert', " ON DUPLICATE KEY UPDATE keywordId=Values(keywordId)");
					
				}
			}
		}
	}
}

?>
